/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.veditor.controller;

import java.awt.event.ActionEvent;
import java.util.HashMap;
import javax.swing.text.Element;
import offset.nodes.client.editor.Editor;
import offset.nodes.client.veditor.VirtualEditor;
import offset.nodes.client.editor.controller.HyperlinkActions.InsertHyperlinkAction;
import offset.nodes.client.editor.model.HyperlinkModel;
import offset.nodes.client.veditor.model.DataModel;
import offset.nodes.client.editor.model.Range;
import offset.nodes.client.editor.view.hyperlink.DataHyperlinkInfo;
import offset.nodes.client.editor.view.hyperlink.HyperlinkDialog;
import offset.nodes.client.veditor.model.VirtualHyperlinkModel;
import offset.nodes.client.veditor.view.hyperlink.VirtualHyperlinkDialog;

/**
 *
 * @author Walter Lütgenau
 */
public class VirtualHyperlinkActions {

    public static class VirtualInsertHyperlinkAction extends InsertHyperlinkAction {

        public VirtualInsertHyperlinkAction(Editor editor) {
            super(editor);
        }
        
        protected DataModel getDataModel() {
            return new DataModel(getDocumentContext(), getEditor().getProperties());
        }   
        
        protected VirtualHyperlinkModel getVirtualModel() {
            return new VirtualHyperlinkModel(getDocumentContext(),getEditor());
        }     
        
        protected void processDataHyperlink(DataHyperlinkInfo dataHyperlink, Element element, int start, int end) throws Exception {
            HashMap queries = (HashMap) getEditor().getProperties().get(VirtualEditor.PROP_SECONDARY_QUERIES);

            String data = dataHyperlink.getNodeTypePrefixName() + "/@" + dataHyperlink.getNameProperty();
            dataHyperlink.setNameProperty(data);

            if (dataHyperlink.getSchemaType() == DataHyperlinkInfo.SCHEMA_NEW) { // secondary Query
                String key = getDataModel().getSecondaryQueryKey(element);
                if (key == null) {
                    int index = getDataModel().getMaximumSecondaryQueryIndex();
                    if (index < queries.size()) { // some secondary query must have been removed in the editor
                        int size = queries.size();
                        for (int i = index + 1; i <= size; i++) {
                            queries.remove(DataModel.SECONDARY_QUERY.substring(1) + i);
                        }
                    }
                    key = DataModel.SECONDARY_QUERY + (index + 1);
                }
                queries.put(key.substring(1), dataHyperlink.getNodeTypePrefixName());
            } else {
                String value = getDataModel().getSecondaryQueryValue(element);
                if (value != null && value.indexOf("/") < 0) { // was a secondary query root
                    getDataModel().removeSecondaryQueries(element);
                    queries.remove(getDataModel().getSecondaryQueryKey(element).substring(1));
                }
            }
            
            getVirtualModel().insertDataHyperlink(new Range(start, end), dataHyperlink, data);
        }

        @Override
        public void action(ActionEvent e) throws Exception {
            VirtualHyperlinkDialog dialog = new VirtualHyperlinkDialog(getEditor());
            if (dialog.showDialog() == HyperlinkDialog.CANCEL) {
                return;
            }

            Range range = new Range(getEditor().getBrowserPane().getSelectionStart(), getEditor().getBrowserPane().getSelectionEnd());
            
            Element element = getModel().getDocument().getParagraphElement(range.getStartOffset());
            
            int type = dialog.getHyperlinkType();
            switch (type) {
                case VirtualHyperlinkDialog.TYPE_DATA:
                    processDataHyperlink(dialog.getDataHyperlinkInfo(), element, range.getStartOffset(), range.getEndOffset());
                    break;
                    
                case HyperlinkDialog.TYPE_INTERNAL:
                    getModel().insertInternalHyperlink(new Range(getEditor().getBrowserPane().getSelectionStart(),
                            getEditor().getBrowserPane().getSelectionEnd()), dialog.getInternalHyperlinkInfo());
                    break;
                    
               case HyperlinkDialog.TYPE_EXTERNAL:
                    getModel().insertExternalHyperlink(new Range(getEditor().getBrowserPane().getSelectionStart(),
                            getEditor().getBrowserPane().getSelectionEnd()), dialog.getExternalHyperlinkInfo());
                    break;
            }
            
            refreshCaret(getEditor(), range);
        }
    }
}
